const Controller = require('../core/base_controller')

class DictionaryController extends Controller {
  async nodes() {
  	const { ctx } = this
  	const client = ctx.grpc.com.jstide.grpc.dictionary.scndDicConfigService
    const { exception, node = {} } = await client.getNode({
      arg: ctx.params.id
    });
  	
  	if (exception) {
      this.exception(exception)
      return
    }
    const {
      items: nodes
    } = node

    this.success(nodes.map(node => ({
      ...node,
      id: node.uuid
    })))
  }

  async roots() {
    const { ctx } = this
    const client = ctx.grpc.com.jstide.grpc.dictionary.scndDicConfigService
    const { exception, nodes } = await client.getAllCategory();
    
    if (exception) {
      this.exception(exception)
      return
    }

    this.success(nodes.map(node => ({
      ...node,
      id: node.uuid
    })))
  }
}

module.exports = DictionaryController;
